(function($, gmwk) {

    function Login(Options) {
  
        // console.info("Options", Options);
    
        var self = this;
        var sClassName = Options.sClassName;
        var jqContainer = Options.jqObjectContainer;
        var Params = {}; 
        var sDefaultLayout;
        
        jqContainer.click(fClick);
        
        console.info("---------> Login constructed");
        
        this.getClassName = function() {
            return sClassName;
        }
        
        this.route = function(Fragment) {
            // Logout
            if (
                (Fragment.a !== undefined) &&
                (Fragment.a === "logout")
            ) {
                fLogout();
                return;
            }
            // Setup parameters for login
            $.extend(true, Params, Fragment);
            if (Fragment.r !== undefined) {
                Params.l = Fragment.r;
            } else {
                Params.l = sDefaultLayout;
            }
            delete Params.r;
            jqContainer.find(".Login_form").show();
        }
        
        this.setDefaultLayout = function() {
          //sDefaultLayout = "";
        }

        function fClick(e) {
          var jqTarget = $(e.target);
          if (gmwk.closestObjectContainer(jqTarget) != jqContainer.attr("id")) {
              return;
          }
          if (jqTarget.is(".Login_login")) {
              gmwk.bAuthorized = true;
              // Load the requested stage, remove request parameter
              gmwk.Router.load(
                  Params,
                  gmwk.Router.PARAMS_REPLACE_STATE
              );
          }
        }
        
        function fLogout() {
          gmwk.bAuthorized = false;
          gmwk.clearStage();
          gmwk.getStageContainer()
              .html("<div class='global_NotFound_content'>You have been logged out. <a href='#l=Dashboard&p=Contact'>Contacts</a></div>");
        }
      
    }
  
    gmwk.classes.Login = Login;

})(jQuery, window.gmwk);
